package com.xbs.utils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
 * 通过ajax局部刷新搜索的车型
 */
import com.xbs.controller.base.BaseServlet;
import com.xbs.entity.CarModelEntity;
import com.xbs.entity.CityEntity;
import com.xbs.entity.PagesEntity;
import com.xbs.service.selfdriving.SelfDrivingService;
@WebServlet("/getcar")
public class AjaxGetCarServlet extends BaseServlet {
	//private List<CarModelEntity> carm;
	private SelfDrivingService sds;
	public AjaxGetCarServlet() {
		//carm = new ArrayList<CarModelEntity>();
		sds = new SelfDrivingService();
	}
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		//获取当前页
		String page = req.getParameter("currpage");
		//获取页大小
		String size = req.getParameter("pagesize");
		//获取用户需要哪一页
		String status = req.getParameter("status");
		
		int currpage=page==null?1:Integer.parseInt(page);
		int pagesize=size==null?4:Integer.parseInt(size);
		
		//车型
		String type = req.getParameter("type");
		//价格
		Double price=Double.parseDouble(req.getParameter("price"));
		Double price2=Double.parseDouble(req.getParameter("price2"));
		
		//品牌
		String pin = req.getParameter("pin");
		
		//总记录数
		int total = sds.selCarModelTotal(type, price, price2, pin);
		//总页数
		int pages = (total-1)/pagesize+1;
		if(status.equals("首页")){
			currpage=1;
		}else if(status.equals("上一页")&&(currpage<=0||currpage>=pages)){
			currpage=1;
		}else if(status.equals("下一页")&&currpage>pages){
			currpage=pages;
		}else if(status.equals("尾页")){
			currpage=pages;
		}
		
		//获取从数据库中查找到的车型
		PagesEntity<CarModelEntity> carm = sds.selCarModel(type,price,price2,pin,currpage,pagesize);
		
		//时间套餐
		String time = req.getParameter("time");
		Double timec = sds.getTimeCombo(time);
	
		String str="";
		if(carm.getData().size()==0||carm.getData()==null){
			str="<p style='font-size:26px;margin:20px 408px;margin-top:50px'>温馨提示</p>"
					+ "<p style='margin-left:340px'>没有符合要求的车辆，请重新筛选</p>";
		}else{
			//遍历查询出来数据集合
			for(CarModelEntity car:carm.getData()){
				str+="<tr class='clist_tr'><td class='pic'>"
						+ "<img src='"+car.getCarmodelimage()+"' width='230' height='130'/>"
						+ "</td><td class='info'>"
						+ "<p class='name'>"+car.getCarmodelname()+"</p>"
						+ "<p class='oth'>三厢|1.6自动|乘坐"+car.getSeating()+"人</p>"
						+ "<div class='alltips'>"
						+ "<i class='tip bh_remen'>热门车</i>"
						+ "<i class='tip bh_xin'>新车型</i>"
						+ "</div>"
						+ "</td>"
						+ "<td class='ord'>"
						+ "<div class='order_box'>"
						+ "<div class='od_price'>"
						+ "<div class='houfu'></div>"
						+ "<div class='car_price' style='z-index: 1;'>"
						+ "<span class='pri_ye'>"
						+ "<em class='rmb'>￥</em>"
						+ "<em class='num'>"+car.getRentcash()*timec+"</em>"
						+ "<em class='unit'>/日均</em>"
						+ "</span>"
						+ "</div>"
						+ "</div>"
						+ "<span class='od_btn'  onclick='sub(\""+car.getCarmodelname()+"\")'>"
						+ "租车</span>"
						+ "</div>"
						+ "</td></tr>";
			}
//			str+="<tr><a href='selfDriving?type="+type+"&price="+price+"&price2="+price+"&pin="+pin+"&page="+currpage+"&size="+pagesize+"'>首页</a>"
//					+ "<a href='selfDriving?type="+type+"&price="+price+"&price2="+price+"&pin="+pin+"&page="+currpage+"&size="+pagesize+"'>上一页</a>"
//							+ "<a href='selfDriving?type="+type+"&price="+price+"&price2="+price+"&pin="+pin+"&page="+currpage+"&size="+pagesize+"'>下一页</a>"
//									+ "<a href='selfDriving?type="+type+"&price="+price+"&price2="+price+"&pin="+pin+"&page="+currpage+"&size="+pagesize+"'>尾页</a></tr>";
//			
		}
		
		resp.getWriter().print(str);
	}
}
